String.prototype.replaceAll = function (s1, s2) {
  return this.replace(new RegExp(s1, "gm"), s2);
}

Date.prototype.format = function (format) {
  var o = {
    'M+': this.getMonth() + 1, //month
    'd+': this.getDate(),    //day
    'h+': this.getHours(),   //hour
    'm+': this.getMinutes(), //minute
    's+': this.getSeconds(), //second
    'q+': Math.floor((this.getMonth() + 3) / 3),  //quarter
    'S': this.getMilliseconds() //millisecond
  }
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1,
      (this.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1,
        RegExp.$1.length == 1 ? o[k] :
          ('00' + o[k]).substr(('' + o[k]).length))
    }
  }
  return format
}

export default {
  query(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
    var r = window.location.search.substr(1).match(reg)

    if (r != null) {
      return unescape(r[2])

    }
    return null
  },

  urlQuery(dict) {
    let query = ''
    for (var key in dict) {
      if (query) {
        query += '&'
      }

      query += key + '=' + dict[key]
    }

    return query
  },

  fixPos(el) {
    let pos = [0, 0]
    if (el) {
      pos[0] += el.offsetHeight * 0.5
      pos[1] += el.offsetWidth * 0.5
    }

    while (el) {
      pos[0] += el.offsetTop - el.scrollTop
      pos[1] += el.offsetLeft - el.scrollLeft
      el = el.offsetParent
    }

    return pos
  },

  intPx(px) {
    return parseInt(px.substr(0, px.length - 2))
  },

  dateTime(time) {
    return new Date(time).toLocaleString().replace(/:\d{1,2}$/, ' ')
    // return new Date(time).format('yyyy-MM-dd hh:mm:ss')
  },

  dateTimeShort(time) {
    return this.dateTime(time * 1000)
  },

  leftTime(time) {
    if (!time || time <= 0) {
      return '00:00:00'

    } else {
      var minute = Math.floor(time / 60)
      var hour = Math.floor(minute / 60)
      var day = Math.floor(hour / 24)
      var second = time - minute * 60
      minute = minute % 60
      hour = hour % 24
      var str = ('0' + hour).slice(-2) + ':' + ('0' + minute).slice(-2) + ':' + ('0' + second).slice(-2)
      if (day > 0) {
        str = day + 'Day ' + str
      }

      return str
    }
  },

  isWeixinBrowser() {
    let agent = navigator.userAgent.toLowerCase()
    if (agent.match(/MicroMessenger/i) == 'micromessenger') {
      return true
    } else {
      return false
    }
  },
  getUrlKey:function(name){
  	return decodeURIComponent((new RegExp('[?|&]'+name+'='+'([^&;]+?)(&|#|;|$)').exec(location.href)||[,""])[1].replace(/\+/g,'%20'))||null;
  } 
}
